package StreamDemo;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Date: 2023-05-01
 * Time: 10:18
 */
public class Test2 {
    public static void main(String[] args) {
        HashMap<String, Integer> hm = new HashMap<>();

        hm.put("zhangsan", 23);
        hm.put("lisi", 24);
        hm.put("wagnwu", 25);

        Set<Map.Entry<String, Integer>> list = hm.entrySet();

        HashMap<String, Integer> newMap = (HashMap<String, Integer>) list.stream().filter(k -> k.getValue() >= 24).
                collect(Collectors.toMap(new Function<Map.Entry<String, Integer>, String>() {
                    @Override
                    public String apply(Map.Entry<String, Integer> stringIntegerEntry) {
                        return stringIntegerEntry.getKey();
                    }
                }, new Function<Map.Entry<String, Integer>, Integer>() {
                    @Override
                    public Integer apply(Map.Entry<String, Integer> stringIntegerEntry) {
                        return stringIntegerEntry.getValue();
                    }
                }));

        System.out.println(newMap);


        ArrayList<String> list1 = new ArrayList<>();

        Collections.addAll(list1,"zhangsan-23","lisi-24","wangwu-25");

        Map<String, Integer> collect = list1.stream().filter(s -> Integer.parseInt(s.split("-")[1]) >= 24).
                collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split("-")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split("-")[1]);
                    }
                }));

        // Lambda 表达式,最终形式,前面的只不过是为了能更好的理解,我相信你能理解我的意思

        Map<String, Integer> collect1 = list1.stream().filter(s -> Integer.parseInt(s.split("-")[1]) >= 24)
                .collect(Collectors.toMap(k -> k.split("-")[0],
                        s -> Integer.parseInt(s.split("-")[1])));

        System.out.println(collect);

        System.out.println(collect1);

    }

}
